package org.midwinter.svnrescue.index.client.def;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.midwinter.svnrescue.index.client.SvnIndexClient;

public class SvnIndexClientDef implements SvnIndexClient{

	private String httpServerPath;
	private HttpClient httpClient;
	
	public SvnIndexClientDef(String httpServerPath){
		this.httpServerPath=httpServerPath;
		HttpConnectionManager httpConnectionManager=new MultiThreadedHttpConnectionManager();
		this.httpClient=new HttpClient(httpConnectionManager);
	}
	
	public void indexRevision(String repositoryPath, String revision) throws IndexRevisionException {
		PostMethod post=new PostMethod(httpServerPath+"/client/svn/index/"+repositoryPath+"/"+revision);
		NameValuePair[] data={
			new NameValuePair("repositoryPath", repositoryPath),
			new NameValuePair("revision",revision)
		};
		post.setRequestBody(data);
		int result;
		try {
			result=httpClient.executeMethod(post);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IndexRevisionException(repositoryPath, revision, e);
		}
		//throws exception if server response error
	}

}
